package com.zipking.cloud.springbootmybatis.controller;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.zipking.cloud.springbootmybatis.config.ApiCoreFactory;
import com.zipking.cloud.springbootmybatis.kafka.KafkaData;
import com.zipking.cloud.springbootmybatis.kafka.TransformFactory;
import com.zipking.cloud.springbootmybatis.prop.DbConf;
import com.zipking.cloud.springbootmybatis.prop.DbProperties;
import com.zipking.cloud.springbootmybatis.redisson.RedisDelayQueueEnum;
import com.zipking.cloud.springbootmybatis.redisson.RedisDelayQueueUtil;
import com.zipking.cloud.springbootmybatis.service.BaseService;
import com.zipking.cloud.springbootmybatis.service.RequestQueue;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.BitFieldSubCommands;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping(value = "/kafka")
public class KafkaController {
    @Autowired
    private RequestQueue requestQueue;

@Autowired
private TransformFactory transformFactory;
    @GetMapping("/testSend")
    public Object testSend() {
        KafkaData kafkaData = new KafkaData();
        kafkaData.setTitle("111");
        kafkaData.setDescription("11");
        kafkaData.setAuthor("111");
        kafkaData.setTags(Lists.newArrayList());
        kafkaData.setMediaName("secondTransform");
        kafkaData.setOssPath("1111");
        kafkaData.setSpiderTime(LocalDateTime.now());
        Map<String,Object> map=Maps.newHashMap();
        map.put("name","张三");
        kafkaData.setRawData(map);

        transformFactory.getMaterial(kafkaData);
        return "ok";
    }
    @GetMapping("/requestQueue")
    public Object requestQueue() {
        for (int i = 0; i < 10000; i++) {
            JSONObject jsonObject=new JSONObject();
            jsonObject.put("userId",i);
            requestQueue.putJsonDataQueue(jsonObject);
        }

        return "ok";
    }

    @Autowired
    private RedisDelayQueueUtil redisDelayQueueUtil;
    @GetMapping("/redis")
    public Object redis(Long time) {
        //加入延迟队列失效
        Map<String, Object> map1 = new HashMap<>();
        map1.put("orderId", "100");
        map1.put("remark", "订单支付超时，自动取消订单");

        redisDelayQueueUtil.addDelayQueue(map1, time, TimeUnit.SECONDS, RedisDelayQueueEnum.ORDER_PAYMENT_TIMEOUT.getCode());

//删除延迟队列
        //redisDelayQueueUtil.removeDelayedQueue(id, RedisDelayQueueEnum.ORDER_TIMEOUT.getCode());


        return "ok";
    }
}
